package net.lzzy.practices.courses;

import com.sun.prism.null3d.NULL3DPipeline;
import io.swagger.annotations.Api;
import net.lzzy.practices.constans.ApiConstant;
import net.lzzy.practices.constans.Messages;
import net.lzzy.practices.entities.Course;
import net.lzzy.practices.entities.User;
import net.lzzy.practices.service.CourseService;
import net.lzzy.practices.utils.StringUtil;
import net.lzzy.practices.web.models.*;
import org.apache.logging.log4j.message.Message;
import org.springframework.web.bind.annotation.*;

import javax.print.attribute.standard.MediaSize;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author Lenovo
 */
@RestController
@CrossOrigin//跨域请求
@RequestMapping(ApiConstant.ROUTE_COURSE_ROOT)//根路由
public class CourseApiController {
    private final CourseService service;
    private final CourseService uService;

    public CourseApiController(CourseService service, CourseService uService) {
        this.service = service;
        this.uService = uService;
    }

    @GetMapping(ApiConstant.ROUTE_COURSE_ALL)//获取数据
    public ApiResult getCourses(@RequestParam String token) {
        List<VmCourse> courses = new ArrayList<>();
        service.get().forEach(course -> courses.add(VmCourse.create(course, false)));
        return new ApiResult(true, courses.size() + "", courses);
    }

    @GetMapping(ApiConstant.ROUTE_COURSE_PAGE)
    public ApiResult getPagedCourse(@RequestParam String token, @RequestParam int page, @RequestParam int size) {
        List<VmCourse> courses = new ArrayList<>();
        service.get(page, size, true).forEach(course -> courses.add(VmCourse.create(course, false)));
        return new ApiResult(true, courses.size() + "", courses);
    }

    @GetMapping(ApiConstant.ROUTE_COURSE_TEACHER)
    public ApiResult getTeacherCourse(@RequestParam String token, @RequestParam(name = "id") UUID teacherId) {
        List<VmCourse> courses = new ArrayList<>();
        service.getTeacherCourses(teacherId).forEach(course -> courses.add(VmCourse.create(course, true)));
        return new ApiResult(true, courses.size() + "", courses);
    }

    @PostMapping(ApiConstant.ROUTE_COURSE_STUDENT)
    public ApiResult getStudentCourses(@RequestParam String token, @RequestParam(name = "id") UUID studentId) {
        List<VmCourse> courses = new ArrayList<>();
        service.getStudentCourses(studentId).forEach(course -> courses.add(VmCourse.create(course, true)));
        return new ApiResult(true, courses.size() + "", true);
    }

    private ApiResult addOrUpdate(UUID id, CourseInfo info) {
        String token = info.getToken();
        String name = info.getName();
        String desc = info.getDesc();
        String cover = info.getCover();
        String teacherId = info.getTid();
        boolean open = info.isOpen();
        if (StringUtil.isEmpty(name) ||
                StringUtil.isEmpty(desc) ||
                StringUtil.isEmpty(cover) ||
                StringUtil.isEmpty(teacherId)) {
            return new ApiResult(false, Messages.MISS_ARGS.toString(), null);
        }
        User teacher = uService.getById(UUID.fromString(teacherId)).getTeacher();
        if (teacher == null) {
            return new ApiResult(false, Messages.WRONG_ID.toString(), null);
        }
        Course course;
        boolean add = id == null;
        if (add) {
            course = new Course();
            course.setTeacher(teacher);
        } else {
            course = service.getById(id);
            if (course == null) {
                return new ApiResult(false, Messages.WRONG_ID.toString(), null);
            }
        }
        course.setUpdateTime(new Date());
        course.setOpen(open);
        course.setName(name);
        course.setCover(cover);
        course.setDescription(desc);
        if (add) {
            return new ApiResult(true, Messages.SUCCESS.toString(), VmCourse.create(service.add(course), true));
        } else {
            Course updated = service.update(course, token);
            if (updated == null) {
                return new ApiResult(false, Messages.NO_PERMISSINO.toString(), null);
            }
        }
        return new ApiResult(true, Messages.SUCCESS.toString(), VmCourse.create(course, true));
    }
    @PostMapping(ApiConstant.ROUTE_COURSE_ADD)
    public ApiResult add(@RequestBody CourseInfo info){
        return addOrUpdate(null,info);
    }
    @PostMapping(ApiConstant.ROUTE_COURSE_PUT)
    public ApiResult update(@RequestBody UUID id,@RequestBody CourseInfo info){
        return addOrUpdate(id,info);
    }
    @PostMapping(ApiConstant.ROUTE_COURSE_OPEN_CLOSE)
    public ApiResult openClose(@RequestBody CourseOpenInfo info){
        String token = info.getToken();
        String strCourseId = info.getCid();
        boolean open = info.isOpen();
        if (StringUtil.isEmpty(token)||StringUtil.isEmpty(strCourseId)){
            return new ApiResult(false,Messages.MISS_ARGS.toString(), null);
        }
        Course course=service.openClose(UUID.fromString(strCourseId),open,token);
        if (course == null){
            return new ApiResult(false,Messages.NO_PERMISSION.toString(),null);
        }
        return new ApiResult(true,Messages.SUCCESS.toString(),VmCourse.create(course,true));
    }
    @DeleteMapping(ApiConstant.ROUTE_COURSE_REMOVE)
    public ApiResult delete(@RequestBody CourseKeyInfo info){
        String token = info.getToken();
        String strCourse = info.getCid();
        if (StringUtil.isEmpty(token) || StringUtil.isEmpty(strCourse)) {
            return new ApiResult(false,Messages.MISS_ARGS.toString(), null);
        }
        try {
            UUID courseId = UUID.fromString(strCourse);
            Messages msg = service.remove(courseId,token);
            return new ApiResult(msg==Messages.SUCCESS, msg.toString(), null);
        }
        catch (Exception e){
            return new ApiResult(false,Messages.WRONG_ID.toString(), null);
        }
    }
}
